<?php
session_start();
include("agoTimeFormat.php");
$myAgoObject = new convertToAgo;
include_once ("../db_connect.php"); // Connect to the database

	// Get the section ID from the url variable coming in
	if (isset($_SESSION['id']) && $_GET['id'] != ""){		$sid = preg_replace('#[^0-9]#i', '', $_GET['id']); // filter all characters except numbers for security	}	else{		echo "Please Login";		exit();	}

// Query the database for that section id, make sure it exists and get the section title
$sql = mysql_query("SELECT * FROM forum_sections WHERE id='$sid' LIMIT 1");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) 
{
echo "ERROR: That section deos not exist you have tampered with our URLs.";
exit();
}
while($row = mysql_fetch_array($sql)){
	$sectionTitle = $row["title"];
}
// Use the section ID to query the "forum_posts" table in the database to get all the threads for this section
$sql = mysql_query("SELECT * FROM forum_posts WHERE type='a' AND section_id='$sid' ORDER BY date_time DESC LIMIT 25");
$dynamiclist = "";
$numRows = mysql_num_rows($sql);
if ($numRows < 1) 
{
$dynamiclist = "There are no threads in this section yet. Be the first to post.";
}
else 
{$replies=-1;
while($row = mysql_fetch_array($sql)){		
	    $thread_id = $row["id"];
		$post_author = $row["post_author"];
		$post_author_id = $row["post_author_id"];				$sql2 = mysql_query("SELECT * FROM forum_posts WHERE post_author_id='$post_author_id' ");		while($row2 = mysql_fetch_array($sql2)){		$replies+=1;		}		
		$date_time = $row["date_time"];
		$convertedTime = $myAgoObject -> convert_datetime($date_time);
        $whenPost = $myAgoObject -> makeAgo($convertedTime);
		$thread_title = $row["thread_title"];
		//$dynamiclist .= '<img src="../images/forumpic/threadPic.jpg" width="26" height="18" alt="Topic" /> ' . $post_author . ' - <a href="view_thread.php?id=' . $thread_id . '">' . $thread_title . '</a> - ' . $date_time . '<br /><br />';		$dynamiclist .=	'<tr><td>'.$post_author.'</td>';		$dynamiclist .=	'		<td><a href="view_thread.php?id=' . $thread_id . '">' . $thread_title . '</a></td>';		$dynamiclist .=	'		<td width="40px"><font size="2">'.$date_time.'</font></td>';		$dynamiclist .=	'		<td width="10px" align="center">'.$replies.'</td></tr>';			
    }
}
?>
</head>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<style type="text/css">
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $sectionTitle; ?> Forum</title>
</head>
	<body>		<div>				<form action="new_topic.php" method="post">							<input name="forum_id" type="hidden" style="margin-left:100px; font-family:Tahoma, Geneva, sans-serif;font-size:24p" value="<?php echo $sid; ?>" />							<input name="forum_title" type="hidden" value="<?php echo $sectionTitle; ?>" />							      <input name="myBtn1" type="submit" style="margin-left:15px" value="Create New Topic"style="background-color:"/>						</form>		</div>		<div ><a href="index.php" style="color:red; font-size:14px; display:block; margin-bottom:5px; margin-left:25px; ">>Forum Home</a></div>
		<table width="900"  border="1" align="center" cellpadding="5" cellspacing="0"style="background-color:#d3e8fb;">			<tr style="background-color:#B40404;">		<td><font color="white" size="4" style="Calibri">Post by:</font></td> <td><font color="white" size="4" style="Calibri">Title</font></th> <td>		<font color="white" size="4" style="Calibri">Timepost</font></th> <td>		<font color="white" size="4" style="Calibri">Replies</font></th></tr>			<?php echo $dynamiclist;?>
		</table>
	</body>
</html>